package com.bestcem.xm.user.handler.excel.read.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.bestcem.xm.user.service.dto.user.ImportUserAccountDTO;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 账号解析
 *
 * @author Linked <zhijian.lin@idiaoyan.com>
 * @version v1.0
 * @date 2022/3/1 14:51
 */
public class UserAccountReadListener<T> extends AnalysisEventListener<T> {

    /**
     * 读取数据List
     */
    private final List<T> data = new LinkedList<>();

    /**
     * 表头map
     */
    private final Map<Integer, String> headMap = new HashMap<>();

    @Override
    public void invoke(T t, AnalysisContext analysisContext) {
        ImportUserAccountDTO dto = (ImportUserAccountDTO) t;
        if (StringUtils.isAllBlank(dto.getEmail(), dto.getUserName())) {
            return;
        }
        data.add(t);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        return;
    }

    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        this.headMap.putAll(headMap);
    }

    /**
     * 获取读取的数据
     *
     * @return List
     */
    public List<T> getResult() {
        return data;
    }

    /**
     * 获取excel表头数据
     *
     * @return Map<Integer, String>
     */
    public Map<Integer, String> getHeadMap() {
        return headMap;
    }
}
